﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CLEX.Service.DataContracts;
using CLEX.Service.Proxies;
using CLEX.Service.ServiceContracts;

namespace CLEX.Client.Metro
{
    public class Facade : IFacade
    {
        private readonly IClientCache clientCache;
        private readonly INavigator navigator;

        public event Action<DatabaseData> DatabaseCreated
        {
            add { clientCache.DatabaseCreated += value; }
            remove { clientCache.DatabaseCreated -= value; }
        }

        public event Action<DatabaseData> DatabaseDeleted
        {
            add { clientCache.DatabaseDeleted += value; }
            remove { clientCache.DatabaseDeleted -= value; }
        }

        public event Action<DatabaseData> DatabaseUpdated
        {
            add { clientCache.DatabaseUpdated += value; }
            remove { clientCache.DatabaseUpdated -= value; }
        }

        public Facade(IClientCache clientCache, INavigator navigator)
        {
            this.clientCache = clientCache;
            this.navigator = navigator;
        }

        public UserData Login(string username, string password)
        {
            return clientCache.Login(username, password);
        }

        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            return clientCache.ChangePassword(username, oldPassword, newPassword);
        }

        public List<DatabaseData> GetDatabasesByUserId(int userId)
        {
            return clientCache.GetDatabasesByUserId(userId);
        }

        public int CreateDatabase(int userId, string connectionString)
        {
            return clientCache.CreateDatabase(userId, connectionString);
        }

        public void DeleteDatabase(int databaseId)
        {
            clientCache.DeleteDatabase(databaseId);
        }

        public void UpdateDatabase(DatabaseData database)
        {
            clientCache.UpdateDatabase(database);
        }

        public DatabaseData GetDatabase(int databaseId)
        {
            return clientCache.GetDatabase(databaseId);
        }

        public List<TableData> GetSchema(string connectionString)
        {
            return clientCache.GetSchema(connectionString);
        }

        public List<HistogramRowData> ComputeHistograms(string connectionString, QueryData queryData)
        {
            return clientCache.ComputeHistograms(connectionString, queryData);
        }

        public void GoToLogin()
        {
            navigator.GoToLogin();
        }

        public void GoToHome(UserData user)
        {
            navigator.GoToHome(user);
        }

        public void GoToLattice(UserData user, DatabaseData database, TableData table)
        {
            navigator.GoToLattice(user, database, table);
        }

        public void ShowLogout()
        {
            navigator.ShowLogout();
        }

        public void ShowDatabaseNew(UserData user)
        {
            navigator.ShowDatabaseNew(user);
        }

        public void ShowTableList(UserData user, DatabaseData database)
        {
            navigator.ShowTableList(user, database);
        }

        public void ShowDatabaseEdit(DatabaseData database)
        {
            navigator.ShowDatabaseEdit(database);
        }

        public void ShowDatabaseDelete(DatabaseData database)
        {
            navigator.ShowDatabaseDelete(database);
        }

        public void ShowChangedPassword(UserData user)
        {
            navigator.ShowChangePassword(user);
        }

        public void ShowLatticeEdit(TableData table, LatticeState state)
        {
            navigator.ShowLatticeEdit(table, state);
        }

        public void ShowHistogramDelete(LatticeState state, AggregateData aggregate)
        {
            navigator.ShowHistogramDelete(state, aggregate);
        }

        public void ShowHistogramNew(TableData table, LatticeState state)
        {
            navigator.ShowHistogramNew(table, state);
        }

        public void ShowUnexpectedError(string errorMessage, string details)
        {
            navigator.ShowUnexpectedError(errorMessage, details);
        }
    }
}
